library(readxl)
library(dplyr)
library(estimatr)
library(jtools)
library(MatchIt)
library(stargazer)
library(mediation)
library(ggplot2)
library(modelsummary)
library(readr)
library(pwr)


study1_exit_poll <- read.csv("wo_data_study_1_psrm.csv") 
study2_qual_data <- read.csv("wo_data_study_2_psrm.csv") 
study1_exit_poll_no_NA <- read.csv("study1_exit_poll_NO_NAs.csv")

  


########
#### STUDY 1
### THREAT  
#### H1 
#### Figure 1 – Effect of Census Information on Senses of Threat
#########
exit_reg1_econthreat <- lm(econthreat ~ Treatment + POLLSTER  + district  + pid + gender + age + income + owneduc, data = study1_exit_poll)
summary(exit_reg1_econthreat)

exit_reg2_polthreat <- lm(polthreat ~ Treatment + POLLSTER + district  + pid + gender + age + income + owneduc, data = study1_exit_poll)
summary(exit_reg2_polthreat)

exit_reg3_cultthreat <- lm(cultthreat ~ Treatment + POLLSTER + district  + pid + gender + age + income + owneduc, data = study1_exit_poll)
summary(exit_reg3_cultthreat)



plot_summs(exit_reg1_econthreat, exit_reg2_polthreat, exit_reg3_cultthreat,
           omit.coefs = c("(Intercept)", "POLLSTER", "district", "pid", "gender", "gender", "age", "income", "owneduc"),
           model.names = c("Economic Threat", "Political Threat", "Cultural Threat"))

## SAVING FIGURE
ggsave("figure1.pdf",
       width = 10,
       height = 10)

### SAVING TABLE
stargazer(exit_reg1_econthreat,exit_reg2_polthreat, exit_reg3_cultthreat, type = "text", out = "Table2.1.tex")







#########
### STUDY 1
#### H2
### Figure 2 – Effect of Asian Racial Threat on Feelings Toward Asian Americans and Immigrants
##########
exit_reg_aafeel <- lm(AsianAmtherm ~ Treatment  + district  + POLLSTER + pid + gender + age + income + owneduc, data = study1_exit_poll)
summary(exit_reg_aafeel)

exit_reg_immfeel <- lm(Immigranttherm ~ Treatment  + district +POLLSTER  + pid + gender + age + income + owneduc, data = study1_exit_poll)
summary(exit_reg_immfeel)

plot_summs(exit_reg_aafeel, exit_reg_immfeel,
           omit.coefs = c("(Intercept)", "POLLSTER", "district", "pid", "gender", "gender", "age", "income", "owneduc"),
           model.names = c("Feelings Toward Asian Americans", "Feelings Toward Immigrants"))
## SAVING FIGURE
ggsave("figure2.pdf",
       width = 10,
       height = 10)

### SAVING TABLE
stargazer(exit_reg_aafeel,exit_reg_immfeel, type = "text", out="Table2.2.tex")



#### STUDY 1
#####  H3 - 4
#### Figure 3 – Effect of Asian Racial Threat on Perceptions of Discrimination
exit_reg_aadiscrim <- lm(aadiscrim ~ Treatment  + district +POLLSTER   + pid + gender + age + income + owneduc, data = study1_exit_poll)
summary(exit_reg_aadiscrim)

exit_reg_aalimit <- lm(aalimit ~ Treatment  + district +POLLSTER  + pid + gender + age + income + owneduc, data = study1_exit_poll)
summary(exit_reg_aalimit)


plot_summs(exit_reg_aadiscrim, exit_reg_aalimit,
           omit.coefs = c("(Intercept)", "POLLSTER", "district", "pid", "gender", "gender", "age", "income", "owneduc"),
           model.names = c("Perception of Discrimination Against AA", "Support for Racial Quota on Asian Applicants"))
## SAVING FIGURE
ggsave("figure3.pdf",
       width = 10,
       height = 10)

### SAVING TABLE
stargazer(exit_reg_aadiscrim,exit_reg_aalimit, type = "text", out="Table2.3.tex")







################
#### STUDY 2
### CULTURAL THREAT, ASIAN, WHITE, IMMIGRANT
### Figure 4 – Cultural Threat Felt from Asian Americans, Immigrants, and White Americans
################
cultthreat_asian= lm(cult_asian ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(cultthreat_asian)

cultthreat_immigrant= lm(cult_immigrants ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(cultthreat_immigrant)

cultthreat_white= lm(cult_white ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(cultthreat_white)

plot_summs(cultthreat_asian, cultthreat_immigrant, cultthreat_white,
           omit.coefs = c("(Intercept)", "age", "gender", "region", "pid", "ideo"),
           model.names = c("Cultural Asian Threat", "Cultural Immigrant Threat", "Cultural White Threat"))
## SAVING FIGURE
ggsave("figure4.pdf",
       width = 10,
       height = 10)


### SAVING TABLE
stargazer(cultthreat_asian,cultthreat_immigrant,cultthreat_white, type = "text", out="Table5.4.tex")





##########
#### STUDY 2
#### Figure 5 -Economic Threat Felt from Asian Americans, Immigrants, and White Americans
##########

econthreat_asian= lm(econ_asian ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(econthreat_asian)

econthreat_immigrants= lm(econ_immigrants ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(econthreat_immigrants)

econthreat_white= lm(econ_white ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(econthreat_white)

plot_summs(econthreat_asian, econthreat_immigrants, econthreat_white,
           omit.coefs = c("(Intercept)", "age", "gender", "region", "pid", "ideo"),
           model.names = c("Economic Asian Threat", "Economic Immigrant Threat", "Economic White Threat"))
## SAVING FIGURE
ggsave("figure5.pdf",
       width = 10,
       height = 10)


### SAVING TABLE
stargazer(econthreat_asian,econthreat_immigrants,econthreat_white, type = "text", out= "Table5.2.tex" )





#########
#### STUDY 2 
### Figure 6 – Political Threat Felt from Asian Americans, Immigrants, and White Americans
##########

polthreat_asian= lm(pol_asian ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(polthreat_asian)

polthreat_immigrants= lm(pol_immigrants ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(polthreat_immigrants)

polthreat_white= lm(pol_white ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(polthreat_white)

plot_summs(polthreat_asian, polthreat_immigrants, polthreat_white,
           omit.coefs = c("(Intercept)", "age", "gender", "region", "pid", "ideo"),
           model.names = c("Political Asian Threat", "Political Immigrant Threat", "Political White Threat"))

## SAVING FIGURE
ggsave("figure6.pdf",
       width = 10,
       height = 10)

### SAVING TABLE
stargazer(polthreat_asian,polthreat_immigrants,polthreat_white, type = "text",
          out="Table5.3.tex")





####### STUDY 2
### H2
### Figure 7 – Feelings Toward Asians, Immigrants, Whites, Blacks, and Hispanics
#########
threat2_asian= lm(feelings_asians ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(threat2_asian)
threat2_immigrants= lm(feelings_immigrants ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(threat2_immigrants)
threat2_white= lm(feelings_whites ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(threat2_white)
threat2_black= lm(feelings_blacks ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(threat2_black)
threat2_hispanics= lm(feelings_hispanics ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(threat2_hispanics)


plot_summs(threat2_asian, threat2_immigrants, threat2_white, threat2_black, threat2_hispanics,
           omit.coefs = c("(Intercept)", "age", "gender", "region", "pid", "ideo"),
           model.names = c("Feelings Toward Asian Americans", "Feelings Toward Immigrants", "Feelings Toward White Americans",
                           "Feelings Toward Black Americans","Feelings Toward Hispanic Americans" ))
## SAVING FIGURE
ggsave("figure7.pdf",
       width = 10,
       height = 10)

### SAVING TABLE
stargazer(threat2_asian,threat2_immigrants,threat2_white,threat2_black,threat2_hispanics, type = "text", out="Table5.7.tex")




########
###### STUDY 2 
#### Figure 8 – Effect of Asian Racial Threat on Perceptions of Discrimination
###########

study2_AAlimit= lm(AA_limit ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(study2_AAlimit)

study2_AAdiscrim = lm(AA_discrim ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(study2_AAdiscrim)

plot_summs(study2_AAdiscrim, study2_AAlimit,
           omit.coefs = c("(Intercept)", "age", "gender", "region", "pid", "ideo"),
           model.names = c("Perception of Discrimination Against AA, Study 2",
                           "Support for Racial Quota on AA, Study 2"))
## SAVING FIGURE
ggsave("figure8.pdf",
       width = 10,
       height = 10)

### SAVING TABLE
stargazer(study2_AAdiscrim,study2_AAlimit, type = "text", out="Table5.5.tex")



#### STUDY 2 
#####  Figure 9 – Effect of Asian Racial Threat on Support for Forcibly Testing Asian Americans and Asian Immigrants for COVID-19 
####
study2_forcibly_test_AA= lm(forcibly_test_asian_americans ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(study2_forcibly_test_AA)

study2_forcibly_test_AImm = lm(forcibly_test_asian_immigrants ~ Treatment + age + gender + region +pid + ideo  , data = study2_qual_data)
summary(study2_forcibly_test_AImm)

plot_summs(study2_forcibly_test_AA, study2_forcibly_test_AImm,
           omit.coefs = c("(Intercept)", "age", "gender", "region", "pid", "ideo"),
           model.names = c("Support for Forcibly Testing Asian Americans", "Support for Forcibly Testing Asian Immigrants" ))
## SAVING FIGURE
ggsave("figure9.pdf",
       width = 10,
       height = 10)



### SAVING TABLE
stargazer(study2_forcibly_test_AA,study2_forcibly_test_AImm, type = "text" , out="Table5.6.tex")

###### APPENDIX
##############
### BALANCE TEST 
#### STUDY 1, TABLE 2.4
##############
balance_test.1 <- matchit(Treatment ~ POLLSTER  + district  + pid + gender + age + income + owneduc +cultthreat+ polthreat+ econthreat, 
                 data = study1_exit_poll_no_NA, replace = TRUE)
summary(balance_test.1)




##############
### BALANCE TEST 
#### STUDY 2, TABLE 5.8
##############
balance_test.2 <- matchit(Treatment ~ age + gender + region +pid + ideo + pol_asian + econ_asian + cult_asian + AA_discrim +AA_limit, 
                 data = study2_qual_data, replace = TRUE)

summary(balance_test.2)


#######
## STUDY 2 MEDIATION IN APPENDIX
### REMOVED ALL NA ROWS
##threat mediating asian american limit
#######


model.m1<- lm(econthreat ~ Treatment + pid + gender + age + income + owneduc, 
              data = study1_exit_poll_no_NA)
model.y1<-  lm(aadiscrim ~ Treatment + econthreat     + pid + gender + age + income + owneduc,
               data = study1_exit_poll_no_NA)

out.1 <- mediate(model.m1, model.y1, sims = 1000, treat = "Treatment",
                 mediator = "econthreat")
summary(out.1)

plot(out.1, main = "Economic Threat Mediating Discrmination Against Asian Americans")


dev.off()
pdf("appendix4.1.pdf",
    width = 10,
    height = 10)
plot(out.1,main = "Economic Threat Mediating Discrmination Against Asian Americans")
dev.off()




model.m2<- lm(econthreat ~ Treatment     + pid + gender + age + income + owneduc, 
               data = study1_exit_poll_no_NA)
model.y2<-  lm(aalimit ~ Treatment + econthreat     + pid + gender + age + income + owneduc,
                data = study1_exit_poll_no_NA)

out.2 <- mediate(model.m2, model.y2, sims = 1000, treat = "Treatment",
                  mediator = "econthreat")
summary(out.2)

plot(out.2,main = "Econ Threat Mediating Effect of Treatment on AA limit")


dev.off()
pdf("appendix4.2.pdf",
    width = 10,
    height = 10)
plot(out.2,main = "Econ Threat Mediating Effect of Treatment on AA limit")
dev.off()


### POWER ANALYSES
###
#### STUDY 1
pwr.t.test(power = 0.8 , d =0.4, sig.level = 0.05 , type = c( "two.sample"))

###
#### STUDY 2
pwr.t.test(power = 0.8 , d =0.3, sig.level = 0.05 , type = c( "two.sample"))




